package com.hengdu.cms.helloword;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Customer {

	public static void main(String[] args) throws IOException, TimeoutException {

		// 创建链接工厂
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("192.168.57.130");
		connectionFactory.setPort(5672);
		connectionFactory.setVirtualHost("/ems");
		connectionFactory.setUsername("ems");
		connectionFactory.setPassword("ems@123");

		// 创建链接对象
		Connection connection = connectionFactory.newConnection();

		// 创建通道
		Channel channel = connection.createChannel();

		// 通道绑定对象
		channel.queueDeclare("Hello", false, false, false, null);

		// 消费消息
		// 参数1: 消费那个队列的消息(队列名称)
		// 参数2: 开始消息的自动确认机制
		// 参数3: 消费时的回调接口
		channel.basicConsume("Hello", true, new DefaultConsumer(channel) {
			// 最后一个参数: 消息队列中取出的消息
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {

				System.out.println(new String(body));
			}
		});

		// 释放资源
//		channel.close();
//		connection.close();
	}

}
